package com.lzhl.buildjar.ui;

import com.lzhl.buildjar.ui.support.BaseDialog;
import com.lzhl.buildjar.ui.support.OperationListener;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2016/3/25
 */
public class NoteUI extends BaseDialog {

    private OperationListener listener;

    private JTextPane tpNote;

    private JButton btnOk;
    private JButton btnCancel;

    public NoteUI(Frame owner, OperationListener listener) {
        super(owner, "说明", true);
        this.listener = listener;

        jbinit();
        init();

        if(owner != null) {
            int width = (int) (owner.getSize().width * 0.8);
            int height = (int) (owner.getSize().height * 0.6);
            setSize(width, height);
        }
        else {
            setSize(600, 400);
        }
        setResizable(true);
        setLocationRelativeTo(owner);
    }

    private void jbinit() {
        Container container = getContentPane();
        container.setLayout(new BorderLayout());

        tpNote = new JTextPane();
        JScrollPane spNote = new JScrollPane(tpNote);
        container.add(spNote, BorderLayout.CENTER);

        Box boxCtrl = Box.createHorizontalBox();
        boxCtrl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        boxCtrl.setPreferredSize(new Dimension(60, 40));
        //不起作用
        boxCtrl.setMaximumSize(new Dimension(Integer.MAX_VALUE, 20));
        boxCtrl.setAlignmentX(Component.LEFT_ALIGNMENT);
        container.add(boxCtrl, BorderLayout.SOUTH);
        btnOk = new JButton("确定");
        btnCancel = new JButton("取消");
        boxCtrl.add(Box.createHorizontalGlue());
        boxCtrl.add(btnOk);
        boxCtrl.add(Box.createHorizontalStrut(5));
        boxCtrl.add(btnCancel);
    }

    private void init() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                tpNote.requestFocus();
            }
        });

        btnOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnOk_actionPerformed(e);
            }
        });

        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                dispose();
            }
        });
    }

    private void btnOk_actionPerformed(ActionEvent e) {
        if(listener != null) {
            String note = tpNote.getText();
            if(note == null) {
                note = "";
            }
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("note", note);
            data.put("position", tpNote.getCaretPosition());
            listener.operationPerformed(data);
        }

        setVisible(false);
        dispose();
    }

    public void setNote(String note, int position) {
        if(note == null) {
            note = "";
        }
        this.tpNote.setText(note);
        if (position >= 0 && position <= note.length()) {
            this.tpNote.setCaretPosition(position);
        }
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception ignore) {
        }

        new NoteUI(null, null).setVisible(true);
    }
}
